<?php

namespace App\Http\Controllers\Api;

use App\Goods;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->goods) { // 商品详情
            $goods = Goods::find($request->goods);
            if ($goods) {
                $goods->kind = $goods->kind->name;
                return $goods;
            }
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => null,
            ]);
        }

        if ($request->craftsman) { // 按匠人检索商品
            switch ($request->status) {
                case 2:
                    return Goods::where('craftsman_id', $request->craftsman)->with(['kind:id,name'])->Paginate(8);
                    break;
                default:
                    return Goods::where([
                        ['craftsman_id', $request->craftsman],
                        ['status', $request->status],
                    ])->with(['kind:id,name'])->Paginate(8);
                    break;
            }
        }

        $goods = Goods::where([
            ['status', '=', 1],
            ['price', '>', $request->min],
            ['price', '<', $request->max],
            ['title', 'like', '%' . $request->key . '%'],
        ]);
        switch ($request->sort) {
            case 1:
                $goods = $goods->orderBy('price', 'asc');
                break;
            case 2:
                $goods = $goods->orderBy('price', 'desc');
                break;
            case 3:
                $goods = $goods->orderBy('order_count', 'asc');
                break;
            case 4:
                $goods = $goods->orderBy('order_count', 'desc');
                break;

            default:
                break;
        }

        if ($request->kind) { // 按分类检索商品
            return $goods->where('kind_id', $request->kind)->Paginate(12);
        }

        return $goods->Paginate(12); // 检索所有商品
    }

    public function switchStatus(Goods $goods)
    {
        $status = $goods->status ? 0 : 1;
        $goods->update(['status' => $status]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function updateOrCreate(Request $request)
    {
        if ($request->id) { // 更新原记录
            Goods::find($request->id)->update([
                'title' => $request->title,
                'desc' => $request->desc,
                'price' => $request->price,
                'amount' => $request->amount,
            ]);
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
            ]);
        }

        $file = $request->file('file');
        $clientName = $_FILES['file']['name'];
        $extension = explode('.', $clientName);

        // 文件名加密
        $newName = md5(date('ymdhis') . $clientName) . "." . $extension[1];
        $file->move("storage/goods/{$request->cid}", $newName);

        return Goods::create(
            [
                'title' => $request->title,
                'craftsman_id' => $request->cid,
                'kind_id' => $request->kind,
                'price' => $request->price,
                'desc' => $request->desc,
                'image_src' => Str::start($newName, "http://craftsman.test/storage/goods/{$request->cid}/"),
                'amount' => $request->amount,
                'update_time' => now()->addHour(8),
            ]
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function destroy(Goods $goods)
    {
        $oldFile = Str::after($goods->image_src, "http://craftsman.test/storage/");
        Storage::delete(Str::finish("public/", $oldFile));
        $goods->delete();
        return 'OK';
    }
}
